package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ycc
 * @date 2022/07/29 16:26:22
 */
@RestController
public class CartsController {

    @Autowired
    ICartService iCartService;

    @GetMapping("shopping/carts")
    public ResponseData showCarts(HttpServletRequest request){
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        Long userId = UserInfoUtil.getUserId(request);
        cartListByIdRequest.setUserId(userId);
        CartListByIdResponse CartListByIdResponse = iCartService.getCartListById(cartListByIdRequest);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(CartListByIdResponse.getCode())) {
            return new ResponseUtil().setData(CartListByIdResponse.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(CartListByIdResponse.getMsg());
    }

    @PostMapping("shopping/carts")
    public ResponseData addCarts(@RequestBody CartForm cartForm){
        AddCartRequest request = CartItemConverter.cartFormToTarget(cartForm, new AddCartRequest());
        AddCartResponse addCartResponse = iCartService.addToCart(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil().setData(addCartResponse.getMsg());
        }
        return new ResponseUtil().setData(addCartResponse.getMsg());
    }

    @PutMapping("shopping/carts")
    public ResponseData updateCarts(@RequestBody CartForm cartForm){
        UpdateCartNumRequest request = CartItemConverter.cartFormToTarget(cartForm,new UpdateCartNumRequest());
        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(request);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(updateCartNumResponse.getCode())) {
            return new ResponseUtil().setData(updateCartNumResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }

    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteProductCarts(@PathVariable("uid")Long uid,
                                           @PathVariable("pid")Long pid){
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
